home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / inheritb.c < prev    next >
C/C++ Source or Header  |  1997-07-22  |  4KB  |  117 lines

  1.  
  2. static char rcsid[] =
  3.     "$Id: inheritb.c,v 1.3 1997/07/09 13:24:52 pvmsrc Exp $";
  4.  
  5. /*
  6.  *         PVM version 3.4:  Parallel Virtual Machine System
  7.  *               University of Tennessee, Knoxville TN.
  8.  *           Oak Ridge National Laboratory, Oak Ridge TN.
  9.  *                   Emory University, Atlanta GA.
  10.  *      Authors:  J. J. Dongarra, G. E. Fagg, M. Fischer
  11.  *          G. A. Geist, J. A. Kohl, R. J. Manchek, P. Mucci,
  12.  *         P. M. Papadopoulos, S. L. Scott, and V. S. Sunderam
  13.  *                   (C) 1997 All Rights Reserved
  14.  *
  15.  *                              NOTICE
  16.  *
  17.  * Permission to use, copy, modify, and distribute this software and
  18.  * its documentation for any purpose and without fee is hereby granted
  19.  * provided that the above copyright notice appear in all copies and
  20.  * that both the copyright notice and this permission notice appear in
  21.  * supporting documentation.
  22.  *
  23.  * Neither the Institutions (Emory University, Oak Ridge National
  24.  * Laboratory, and University of Tennessee) nor the Authors make any
  25.  * representations about the suitability of this software for any
  26.  * purpose.  This software is provided ``as is'' without express or
  27.  * implied warranty.
  28.  *
  29.  * PVM version 3 was funded in part by the U.S. Department of Energy,
  30.  * the National Science Foundation and the State of Tennessee.
  31.  */
  32.  
  33. /*
  34.  *    Filename:     inheritb.c
  35.  *
  36.  *  This example shows how a generic receive any message from any task
  37.  *   may "select" the message to receive based on the context.
  38.  *
  39.  *  inheritb starts two children (2 x inherit2), each under their own
  40.  *   context so that the parent (inheritb) may receive communication
  41.  *   based on the child's context.
  42.  *
  43.  *  files used:  inheritb.c inherit2.c
  44.  *
  45.  *  usage:  inheritb
  46.  *
  47.  */
  48.  
  49. #include <stdio.h>
  50. #ifndef WIN32
  51. #include <unistd.h>        /* for gethostname */
  52. #else
  53. #include "pvmwin.h"
  54. #endif
  55. #include "pvm3.h"
  56.  
  57. main()
  58. {
  59.     char *me = "inheritb";
  60.     int context_original, context_1, context_2;
  61.     int ptid, tid, gptid, cc;
  62.     char buf[100];
  63.     char machine[25];
  64.     int i=0;
  65.  
  66.     gethostname( machine, 25 );
  67.  
  68.     printf( "%d:%s: t%x on machine <%s> with context %d.\n",
  69.             i++, me, pvm_mytid(), machine, pvm_getcontext() );
  70.  
  71.     context_original = pvm_getcontext(); /* retrieve initial context */
  72.  
  73.     context_1 = pvm_newcontext();         /* get new context 1 */
  74.     pvm_setcontext( context_1 );         /* activate new context 1 */
  75.  
  76.     printf( "%d:%s: t%x on machine <%s> with new #1 context %d ",
  77.             i++, me, pvm_mytid(), machine, pvm_getcontext() );
  78.     printf( "--> spawn 1st inherit2.\n" );
  79.  
  80.     /* start 1st inherit2 worker @ context 1 */
  81.     pvm_spawn( "inherit2", (char **) 0, PvmTaskDefault, "", 1, &tid );
  82.  
  83.     context_2 = pvm_newcontext();         /* get new context 2 */
  84.     pvm_setcontext(context_2);             /* activate new context 2 */
  85.  
  86.     printf( "%d:%s: t%x on machine <%s> with new #2 context %d ",
  87.             i++, me, pvm_mytid(), machine, pvm_getcontext() );
  88.     printf( "--> spawn 2nd inherit2.\n" );
  89.  
  90.     /* start 2nd inherit2 worker @ context 2 */
  91.     pvm_spawn( "inherit2", (char **) 0, PvmTaskDefault, "", 1, &tid );
  92.  
  93.     /* receive from inherit2 worker 2 @ context 2 */
  94.     cc = pvm_recv( -1, -1 );
  95.     pvm_bufinfo( cc, (int *) 0, (int *) 0, &tid );
  96.     pvm_upkstr( buf );
  97.     printf( "%d:%s: t%x %s <-- received from 2nd inherit2.\n",
  98.             i++, me, tid, buf);
  99.  
  100.     /* reactivate initial context 1*/
  101.     pvm_setcontext(context_1);
  102.  
  103.     printf( "%d:%s: t%x on machine <%s> with new #1 context %d.\n",
  104.             i++, me, pvm_mytid(), machine, pvm_getcontext() );
  105.  
  106.     /* receive from inherit2 worker 1 @ context 1 */
  107.     cc = pvm_recv( -1, -1 );
  108.     pvm_bufinfo( cc, (int *) 0, (int *) 0, &tid );
  109.     pvm_upkstr( buf );
  110.     printf( "%d:%s: t%x %s <-- received from 1st inherit2.\n",
  111.             i++, me, tid, buf );
  112.  
  113.     pvm_exit();
  114.     exit( 0 );
  115. }
  116.  
  117.